package org.yookiee.support.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.yookiee.support.core.CustomObjectMapper;
import org.yookiee.support.core.deserializers.CustomLocalDateDeserializer;
import org.yookiee.support.core.deserializers.CustomLocalDateTimeDeserializer;
import org.yookiee.support.core.deserializers.CustomLocalTimeDeserializer;
import org.yookiee.support.core.serializers.CustomLocalDateSerializer;
import org.yookiee.support.core.serializers.CustomLocalDateTimeSerializer;
import org.yookiee.support.core.serializers.CustomLocalTimeSerializer;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import org.yookiee.support.config.interceptors.EasyI18nInterceptor;
import java.util.*;

/**
 * FileName TimezoneConvertConfiguration
 * Package org.jeecg.config.i18n.config
 * Description
 * author cpstory
 * create 2023-11-15 16:13
 * version V1.0
 */
@Order(Integer.MIN_VALUE)
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class TimezoneConvertConfiguration implements WebMvcConfigurer {

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");

    @Resource
    @Lazy
    private MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter;

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        ObjectMapper objectMapper = jacksonObjectMapper();
        JavaTimeModule javaTimeModule = new JavaTimeModule();   // 默认序列化没有实现，反序列化有实现
        //自定义的反序列化
        javaTimeModule.addSerializer(LocalDateTime.class, new CustomLocalDateTimeSerializer(DATE_TIME_FORMATTER));//目前应该只使用了这个
        javaTimeModule.addSerializer(LocalDate.class, new CustomLocalDateSerializer(DATE_FORMATTER));
        javaTimeModule.addSerializer(LocalTime.class, new CustomLocalTimeSerializer(TIME_FORMATTER));

        //自定义的序列化
        javaTimeModule.addDeserializer(LocalDateTime.class,new CustomLocalDateTimeDeserializer(DATE_TIME_FORMATTER));//目前应该只使用了这个
        javaTimeModule.addDeserializer(LocalDate.class,new CustomLocalDateDeserializer(DATE_FORMATTER));
        javaTimeModule.addDeserializer(LocalTime.class,new CustomLocalTimeDeserializer(TIME_FORMATTER));
        objectMapper.registerModule(javaTimeModule);
        // 设置时区
        objectMapper.setTimeZone(TimeZone.getDefault());
        if(mappingJackson2HttpMessageConverter == null){
            mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        }
        mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);

        Iterator<HttpMessageConverter<?>> iterator = converters.iterator();
        while (iterator.hasNext()){
            HttpMessageConverter<?> converter = iterator.next();
            if(converter instanceof MappingJackson2HttpMessageConverter){
                iterator.remove();
            }
        }

        converters.add(0, mappingJackson2HttpMessageConverter);
    }

    @Bean
    @Primary
    public ObjectMapper jacksonObjectMapper(){
        CustomObjectMapper objectMapper = new CustomObjectMapper();
        //取消时间的转化格式，默认是时间戳,同时需要设置要表现的时间格式
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        objectMapper.configure(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS, false);
        //处理bigDecimal
        objectMapper.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN);
        objectMapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
        //处理失败
        objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
        objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES, false);
        return objectMapper;
    }

    @Primary
    @Bean
    public EasyI18nInterceptor easyI18nInterceptor() {
        return new EasyI18nInterceptor();
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(easyI18nInterceptor()).addPathPatterns("/**");
    }

}
